import { resourceLibraryFileMock } from './data/library.file'
import Mock from 'mockjs'
import { getNowDateTime } from '../../utils'
import { getMimetype, getFileExt } from './helper'
let resourceLibraryFileList = resourceLibraryFileMock(400)
export default [
  {
    url: '/infra/resource-library-file/create', // 新增
    method: 'post',
    response: data => {
      insert(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/infra/resource-library-file/update', // 修改
    method: 'post',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/infra/resource-library-file/rename', // 重命名
    method: 'put',
    response: data => {
      updateById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/infra/resource-library-file/move', // 移动
    method: 'put',
    response: data => {
      updateFolderByIds(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-file/delete' + '.*'), // 删除
    method: 'delete',
    response: data => {
      deleteByIds(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-file/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-file/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-file/check-quote' + '.*'), // 检查引用
    method: 'post',
    response: data => {
      updateQuoteByIds(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-file/presigned-url' + '.*'), // 获取预签名地址
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getPresignedUrl()
      }
    }
  },
  {
    url: '/infra/resource-library-file/upload', // 单文件上传
    method: 'post',
    response: data => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getUploadFileUrl(data)
      }
    }
  },
  {
    url: '/infra/resource-library-file/uploads', // 多文件上传
    method: 'post',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  }
]

const getListByPage = params => {
  let result = resourceLibraryFileList
  result = result.filter(item => !item.deleted)
  if (params.query.name) {
    result = result.filter(item => item.name.indexOf(params.query.name) !== -1)
  }
  if (params.query.folderId) {
    result = result.filter(item => item.folderId === parseInt(params.query.folderId))
  }
  if (params.query.mimetype) {
    result = result.filter(item => params.query.mimetype === item.mimetype)
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const insert = data => {
  resourceLibraryFileList.unshift({
    ...data.body,
    id: Mock.mock('@natural(100, 99999)'),
    creator: '1',
    create_time: getNowDateTime(),
    updater: '',
    update_time: '',
    deleted: false,
    deleted_time: ''
  })
}

const updateById = data => {
  resourceLibraryFileList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const updateQuoteByIds = data => {
  if (data.body.ids.length > 0) {
    resourceLibraryFileList.forEach(item => {
      const hasItem = data.body.ids.some(v => {
        return v === item.id
      })
      if (hasItem) {
        item.quote = Mock.mock('@natural(0, 1)')
      }
    })
  } else {
    resourceLibraryFileList.forEach(item => {
      item.quote = Mock.mock('@natural(0, 1)')
    })
  }
}

const updateFolderByIds = data => {
  resourceLibraryFileList.forEach(item => {
    const hasItem = data.body.ids.some(v => {
      return v === item.id
    })
    if (hasItem) {
      item.folderId = data.body.folderId
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const deleteByIds = data => {
  resourceLibraryFileList.forEach(item => {
    const hasItem = data.body.ids.some(v => {
      return v === item.id
    })
    if (hasItem) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}

const getInfoById = params => {
  const result = resourceLibraryFileList.filter(item => {
    return item.id === parseInt(params.query.id)
  })
  if (result.length > 0) {
    if (result[0].mimetype === 'image') {
      result[0].url = 'https://img1.baidu.com/it/u=1919046953,770482211&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
    }
    return result[0]
  } else {
    return null
  }
}

const getPresignedUrl = () => {
  const extList = ['png', 'jpg', 'jpeg', 'gif', 'mp4', 'm3u8', 'mp3', 'xlsx', 'zip', 'docx', 'pdf', 'html', 'css', 'js']
  return {
    // 文件配置编号
    configId: 11,
    // 文件上传 URL
    uploadUrl: Mock.mock('@url()'),
    // 文件 URL
    url: Mock.mock('@url()') + '/' + Mock.mock('@word(32)') + '.' + extList[Mock.mock('@natural(0, 13)')]
  }
}

const getUploadFileUrl = data => {
  const mimetype = getMimetype(data.body.name)
  const ext = getFileExt(data.body.name)
  let url = ''
  if (mimetype === 'image') {
    const width = Mock.mock('@natural(100, 1920)')
    const height = Mock.mock('@natural(100, 1080)')
    url = Mock.Random.image(width + 'x' + height, Mock.mock('@color()'), '#FFF', ext, Mock.mock('@character()'))
  } else {
    const domain = Mock.mock('@url()')
    const path = Mock.mock('@word(32)')
    url = domain + '/' + path + '.' + ext
  }
  return {
    url,
    name: data.body.name
  }
}
